<template>
  <div id="user-info-edit-page">
    <a-form title="修改资料">
      <a-form-item label="昵称">
        <a-input v-model:value="user.userName" />
      </a-form-item>
      <a-form-item label="头像">
        <a-upload
          v-model:file-list="fileList"
          name="avatar"
          list-type="picture-card"
          class="avatar-uploader"
          :show-upload-list="false"
          :before-upload="beforeUpload"
        >
          <img v-if="user.userAvatar" :src="user.userAvatar" style="width: 100px;height: 100px; object-fit: cover" alt="avatar" />
          <div v-else>
            <loading-outlined v-if="loading"></loading-outlined>
            <plus-outlined v-else></plus-outlined>
            <div class="ant-upload-text">上传</div>
          </div>
        </a-upload>
      </a-form-item>
      <a-form-item label="简介">
        <a-input type="textarea" v-model:value="user.userProfile" />
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getUserVoByIdUsingGet } from '@/api/userController.ts'
import { useUserStore } from '@/stores/user.ts'
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons-vue'
import type { UploadProps } from 'ant-design-vue'
import { message } from 'ant-design-vue'

const userStore = useUserStore()

const user = ref<API.UserVO>({})
// 查询用户
const getUser = async () => {
  const res = await getUserVoByIdUsingGet({
    id: userStore.userInfo?.id,
  })
  if (res.data.code != 0) {
    return
  }
  user.value = res.data.data
}

const fileList = ref([])
const loading = ref<boolean>(false)
const imageUrl = ref<string>('')

const beforeUpload = (file: UploadProps['fileList'][number]) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
  if (!isJpgOrPng) {
    message.error('文件格式不正确!')
  }
  const isLt2M = file.size / 1024 / 1024 < 2
  if (!isLt2M) {
    message.error('文件大小不超过2MB!')
  }
  return isJpgOrPng && isLt2M
}

onMounted(() => {
  getUser()
})
</script>

<style scoped lang="scss">
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}

.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
